package com.pc.controller;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pc.mapper.JsoupPageUrlMapper;
import com.pc.pojo.PageUrl;
import com.pc.pojo.StaticVariable;
import com.pc.util.log.LogWrite;
import com.pc.util.rabbitmq.RabbitmqConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

@Controller
public class GetMsgController {
	
	@Autowired
	private RabbitmqConnection rabbitmqConnection;
	@Autowired
	private JsoupPageUrlMapper jsoupMapper;
	
	@Autowired
	private StaticVariable staticVariable;
	
	@RequestMapping("getMsg1/{num}")
	public void getMsg1(@PathVariable Integer num,HttpServletResponse response)  {
		//大于5万阻塞
		if(num>=(staticVariable.endSizePC_1*10000)) {
			try {
				LogWrite.error(GetMsgController.class, "sleep");
				Thread.sleep(1000*60*60);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return ;
		}
		//LogWrite.trace(GetMsgController.class,"getMsg1()");
		Connection conn = rabbitmqConnection.getConn();
		Integer startIndex=num;
		Channel chan =null;
		List<PageUrl> list=null;
		try {
			chan = conn.createChannel();
			// 声明交换机
			// 参数意义,1 交换机名称,2 类型:fanout,direct,topic
			chan.exchangeDeclare("directEx", "direct");
			Integer pageSize=staticVariable.pageSizePC_1;
//			Integer count = jsoupMapper.queryAllNum();
//			while(true) {
				LogWrite.warn(GetMsgController.class,"开始从数据库取数据：目前位置startIndex="+startIndex);
				list = jsoupMapper.queryPage(startIndex, pageSize);
				for(PageUrl pojo:list) {
					// 发送消息
					String msg = pojo.getId()+","+pojo.getUrl();
					LogWrite.warn(GetMsgController.class,"获取到数据内容"+pojo);//------------------------------
					chan.basicPublish("directEx", "pc01", null, msg.getBytes());
					LogWrite.trace(GetMsgController.class,"发送完毕");
				}
//				LogWrite.warn(GetMsgController.class,"开始阻塞2分钟");
//				Thread.sleep(1000);
//				LogWrite.warn(GetMsgController.class,"阻塞2分钟结束");
//				if(count<=(startIndex+list.size())) {
//					LogWrite.error(GetMsgController.class,"数据库已读取完毕当前startIndex="+startIndex);
//					LogWrite.error(GetMsgController.class,"开始阻塞2分钟");
//					//阻塞2分钟/重定向继续查询
//					Thread.sleep(1000*60*2);
//					LogWrite.error(GetMsgController.class,"阻塞2分钟结束");
//					response.sendRedirect(String.valueOf(startIndex+list.size()));
//					LogWrite.error(GetMsgController.class,"数据库已读取完毕当前startIndex="+String.valueOf(startIndex+list.size()));
//				}
//				startIndex +=list.size();
//			}
		} catch (Exception e) {
			LogWrite.error(GetMsgController.class,"异常退出--"+e.getMessage());
		}finally{
			try {
				chan.close();
				conn.close();
//				startIndex +=list.size();
				LogWrite.error(GetMsgController.class,"出现异常重定向到startIndex="+startIndex);
//				response.sendRedirect(String.valueOf(startIndex));
			} catch (IOException e1) {
				e1.printStackTrace();
			} catch (TimeoutException e1) {
				e1.printStackTrace();
			}
		}
	}
}
